package com.handpay.zztong.hp.ui;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.bean.Banner;

/**
 * 对自定义组件AdGallery进行了一次封装 包含对图片标题和当前位置指示器(RadioGroup)的操作
 */
public class AdGalleryHelper {

    private AdGallery mAdGallery; // 无限滚动Gallery
    private newCirclePageIndicator pageIndicator; // 滚动标记组件
    private Context mContext;
    private LayoutInflater mInflater;
    private RelativeLayout galleryLayout;
    private int width = 0;
    private int height = 0;

    public AdGalleryHelper(Context context, final Banner[] ads,                           long switchTime, boolean isAutoSwitch, int width, int height) {
        this.mContext = context;
        this.width = width;
        this.height = height;
        //width=540,height=960
        Log.i("result", "width=" + width + ",height=" + height);
        mInflater = LayoutInflater.from(context);
        galleryLayout = (RelativeLayout) mInflater.inflate(
                R.layout.adgallery_hellper, null);

        pageIndicator = (newCirclePageIndicator) galleryLayout
                .findViewById(R.id.home_pop_gallery_mark);
        if (width <= 540 || height <= 960) {
            pageIndicator.setDefaultCircleSize(6);
            pageIndicator.setSelectCicleSize(3);
        }
        mAdGallery = (AdGallery) galleryLayout.findViewById(R.id.gallerypop);
        if (ads != null) {
            pageIndicator.setTotalPage(ads.length);
            if (ads.length <= 1) {
                pageIndicator.setVisibility(View.GONE);
            }
            if (ads.length > 1) {
                mAdGallery.setIsCanScorlling(false);
            } else {
                mAdGallery.setIsCanScorlling(true);
            }
            mAdGallery.isAutoSwitch = isAutoSwitch;
            mAdGallery.init(ads, switchTime, new OnGallerySwitchListener() {
                @Override
                public void onGallerySwitch(int position) {
                    if (pageIndicator != null && ads.length > 1) {
                        pageIndicator.setCurrentPage(position);
                    }
                }
            }, mAdGallery.isAutoSwitch, height);
        }
    }

    /**
     * 向外开放布局对象，使得可以将布局对象添加到外部的布局中去
     *
     * @return
     */
    public RelativeLayout getLayout() {
        return galleryLayout;
    }

    /**
     * 获取当前使用的AdGllery对象
     *
     * @return
     */
    public AdGallery getAdGallery() {
        return mAdGallery;
    }

    /**
     * 开始自动循环切换
     */
    public void startAutoSwitch() {
        mAdGallery.setRunFlag(true);
        mAdGallery.startAutoScroll();
    }

    /**
     * 停止自动循环切换
     */
    public void stopAutoSwitch() {
        mAdGallery.setRunFlag(true);
    }

    /**
     * 图片切换回调接口
     */
    public interface OnGallerySwitchListener {
        void onGallerySwitch(int position);
    }

}
